package generic.net.observers;

import generic.net.Message;
import generic.net.Peer;
import java.util.Vector;

/**
 * Notify observers of message arrival.
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class MessageDispatcher {
    private Vector<MessageObserver> observers = new Vector<MessageObserver>();

    /**
     * add yourself as an observer
     * @param observer you.
     */
    public synchronized void add(MessageObserver observer) {
        observers.add(observer);
    }

    /**
     * look away
     * @param observer you.
     */
    public synchronized void remove(MessageObserver observer) {
        observers.remove(observer);
    }

    /**
     *
     * @param peer
     * @param message
     */
    public synchronized void notifyMessageReceived(Peer peer, Message message) {
        for (MessageObserver observer: observers) {
            observer.messageReceived(peer, message);
        }
    }

    public synchronized int size() {
        return observers.size();
    }
}
